home *** CD-ROM | disk | FTP | other *** search
- /* $VER: Awari.dopus5 1.1 (7.6.98) by D.Clarke
-
- The FIRST game in a DOpus lister!
-
- A W A R I
- ~~~~~~~~~~~~~~~~~
-
- My Side
-
- | 6 | 5 | 4 | 3 | 2 | 1 |
- |ooo|ooo|ooo|ooo|ooo|ooo|
- My home | | | | | | | Your home
- |ooo|ooo|ooo|ooo|ooo|ooo|
- | 1 | 2 | 3 | 4 | 5 | 6 |
-
- Your Side
-
- Awari is an African game played with seven sticks and
- thirty-six stones or beans laid out as shown above. The
- board is divided into six compartments or pits on each side.
- In addition, there are two special HOME pits at the end.
-
- A move is made by taking all the beans from any
- (non-empty) pit on your own side. Starting from the pit to
- the right of this one, these beans are "sown" one in each
- pit working around the board anticlockwise.
-
- A turn consists of one or two moves. If the last bean of
- your move is sown in your own home you may take a second
- move.
-
- If the last bean sown in a move lands in an empty pit,
- provided that the opposite pit is not empty, all the beans
- in the opposite pit, together with the last bean sown are
- "captured" and moved to the players home.
-
- When either side is empty, the game is finished. The
- player with the most beans in his home has won.
-
- In the computer version, the board is printed as 14
- numbers representing the 14 pits.
-
- 3 3 3 3 3 3
- 0 0
- 3 3 3 3 3 3
-
- The pits on your (lower) side are numbered 1-6 from left
- to right. The pits on my side (computer) are numbered from
- my left (your right).
-
- When it is your move, the Status bar will display
- "Your Move:". To make a move you select a number from the
- toolbar, if the move is valid the board will be updated.
-
- If the last bean lands in your home the computer shows
- "Again:" in the Status bar and you can select another move.
-
- The computer move is shown below the board, which will be
- shown in it's new state. If the computer was able to make a
- second move, it will be shown as 'x,y'. The computer always
- offers you the first move. This is considered to be a slight
- advantage.
-
- There is a learning mechanism in the program that causes
- the play of the computer to improve as it plays more games.
-
- */
- options results
- address 'DOPUS.1'
- if ~show('L','rexxsupport.library') then call addlib('rexxsupport.library',0,-30)
- dopus version
- version = result ~= 'RESULT' & translate(result,'.',' ') >= 5.1538
- if ~version then do
- dopus request '"Sorry, requires V5.7+ of DOpus" OK'
- exit
- end
- alltraps = 'Awari1 Awari2 Awari3 Awari4 Awari5 Awari6 AwariHelp AwariExit'
- plural. = 's'
- plural.1 = ''
- lf = '0a'x
- n = 0
- computer = 0
- human = 0
-
- call openport('awari-handler')
- lister new '258/353/240/120'
- AHandle = result
- lister set AHandle busy on
- lister set AHandle display name
- lister set AHandle sort name
- lister set AHandle handler 'awari-handler'
- lister set AHandle toolbar 'Awari.tb'
- lister set AHandle title 'DOpus Awari'
- traps = alltraps
- do while traps ~= ''
- parse var traps trapcommand traps
- dopus command '"'trapcommand'"' temp private handler
- dopus addtrap '"'trapcommand'"' 'awari-handler'
- end
- lister set AHandle busy off
-
- if open('varfile','DOpus5:System/Awari.var','r') then do
- scores = readln('varfile')
- do until eof('varfile')
- interpret readln('varfile')
- end
- call close('varfile')
- computer = value(strip(left(scores,4)))
- human = value(strip(right(scores,4)))
- end
-
- Begin:
- lister remove AHandle '4'
- e = 0
- b. = 3
- c = 0
- f.n = 0
- b.6 = 0
- b.13 = 0
-
- do forever
- call PrintIt
- lister set AHandle header 'Your Move:'
- lister refresh AHandle full
- call GetHumanInput
- call MakeMove
- call PrintIt
- if ~e then leave
- if m = 6 then do
- lister set AHandle header 'Again:'
- lister refresh AHandle full
- call GetHumanInput
- call MakeMove
- call PrintIt
- end
- if ~e then leave
- Move = 'My move is: '
- call CPU_move
- if ~e then leave
- if m = 13 then do
- Move = Move || ','
- call CPU_move
- end
- if ~e then leave
- fileinfo.name = '4'
- fileinfo.display = Move
- fileinfo.type = -4
- lister addstem AHandle fileinfo.
- lister refresh AHandle
- end
-
- if h = 13 then call PrintIt
-
- EndGame = 'Game Over'
- d = b.6 - b.13
- if d = 0 then EndGame = EndGame||lf||'Drawn game...'
- else do
- n = n + 1
- if d > 0 then do
- EndGame = EndGame||lf||'You win by 'd' point'plural.d
- human = human + 1
- end
- else do
- d = abs(d)
- EndGame = EndGame||lf||'I win by 'd' point'plural.d
- computer = computer + 1
- end
- end
- lister request AHandle '"'EndGame'" OK'
- signal Begin
-
- MakeMove:
- k = m
- call Moveit m
- e = 0
- if k > 6 then k = k - 7
- c = c + 1
- if c < 9 then f.n = f.n * 6 + k
- do i = 7 to 12
- if b.i ~= 0 then do
- do j = 0 to 5
- if b.j ~= 0 then do
- e = 1
- return
- end
- end
- end
- end
- return
-
- Moveit: procedure expose b. h m
- parse arg m
- p = b.m
- b.m = 0
- do p
- m = m + 1
- if m > 13 then m = m - 14
- b.m = b.m + 1
- end
- p = 12 - m
- if b.m = 1 & m ~= 6 & m ~= 13 & b.p ~= 0 then do
- b.h = b.h + b.p + 1
- b.m = 0
- b.p = 0
- end
- return
-
- CPU_Move:
- h = 13
- d = -99
- q = -99
- do i = 0 to 13
- g.i = b.i
- end
-
- do j = 7 to 12
- if b.j = 0 then iterate
- g = 0
- call Moveit j
- do i = 0 to 5
- if b.i = 0 then iterate
- l = b.i + i
- r = (l > 13)
- l = l // 14
- p = 12 - l
- if b.l = 0 & l ~= 6 & l ~= 13 then r = b.p + r
- if r > q then q = r
- end
- q = b.13 - b.6 - q
- if c < 9 then do
- k = j
- if k > 6 then k = k - 7
- do i = 0 to n - 1
- if f.n * 6 + k = trunc(f.i / 6 ** (7 - c) + 0.1) then q = q - 2
- end
- end
- do i = 0 to 13
- b.i = g.i
- end
- if q >= d then do
- a = j
- d = q
- end
- end
-
- m = a
- Move = Move (m - 6)
- call MakeMove
- return
-
- PrintIt:
- p = ' '
- do i = 12 to 7 by -1
- p = p||Format(b.i,4,' ')
- end
- fileinfo.name = 1
- fileinfo.display = p
- fileinfo.type = -1
- lister addstem AHandle fileinfo.
- fileinfo.name = 2
- fileinfo.display = ' '||Format(b.13,2,' ')||' '||Format(b.6,2,' ')
- fileinfo.type = -2
- lister addstem AHandle fileinfo.
- p = ' '
- do i = 0 to 5
- p = p||Format(b.i,4,' ')
- end
- fileinfo.name = 3
- fileinfo.display = p
- fileinfo.type = -1
- lister addstem AHandle fileinfo.
- lister refresh AHandle
- return
-
- GetHumanInput:
- valid = 0
- do until valid
- if waitpkt('awari-handler') then do
- packet = getpkt('awari-handler')
- if packet ~= '00000000'x then do
- event = strip(getarg(packet,0))
- if event = 'inactive' then signal TheExit
- if event = 'AwariExit' then signal TheExit
- if event = 'AwariHelp' then call Help
- if pos(event,alltraps) = 0 then do
- text = 'Unsupported command, please use'||lf||'the toolbar for input.'
- lister request AHandle '"'text'" OK'
- valid = 0
- end
- else if event ~= 'AwariHelp' then do
- m = value(right(event,1)) - 1
- if b.m = 0 then do
- text = 'Illegal move: 'm + 1' is empty.'
- lister request AHandle '"'text'" OK'
- valid = 0
- end
- else valid = 1
- end
- end
- h = 6
- call reply(packet,0)
- end
- end
- return
-
- Format: procedure
- parse arg zz,num,str
- pp = reverse(overlay(reverse(trunc(zz + .5)),copies(str,num)))
- return pp
-
- Help:
- if ~open('helpfile','T:Awari.help','w') then do
- text = "Error: Unable to create help file"||lf||"''T:Awari.help''"||lf||"You''re on your own :)"
- lister request AHandle '"'text'" OK'
- return
- end
- else do
- do Line = 5 by 1
- if left(Sourceline(Line),2) = '*/' then do
- call close('helpfile')
- dopus read 'T:Awari.help'
- readhandle = result
- return
- end
- call writeln('helpfile',Sourceline(Line))
- end
- end
-
- TheExit:
- text = 'Thanks for playing'||lf||'DOpus Awari.'
- text = text||lf||lf||'Current Scores:'||lf||'Computer: 'computer||lf||'Human: 'human||lf
- dopus request '"'text'" OK'
- if open('varfile','DOpus5:System/Awari.var','w') then do
- scores = computer||' '||human
- call writeln('varfile',scores)
- call writeln('varfile','n = 'n)
- do i = 0 to n
- call writeln('varfile', 'f.'i' = "'f.i'"')
- end
- call close('varfile')
- call delete('T:Awari.help')
- end
- traps = alltraps
- do while traps ~= ''
- parse var traps trapcommand traps
- dopus remtrap '"'trapcommand'"' 'awari-handler'
- dopus command '"'trapcommand'"' temp remove
- end
- call closeport('awari-handler')
- if event ~= 'inactive' then lister close AHandle
- if readhandle ~= 0 then dopus read readhandle quit
- exit
-